package com.li.shop.controller;

import com.li.shop.domain.Merchants;
import com.li.shop.service.MerchantsService;
import com.li.shop.utils.JavaMailUtils;
import com.li.shop.utils.SearchMerchantsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Random;

@Controller
@RequestMapping("/merchants")

public class MerchantsController {

    @Autowired
    private MerchantsService merchantsService;

   /* @RequestMapping("/preLogin")
    public String hello(){
        return "before/login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String userLogin(String userName, String password, HttpSession session){
        User user = userService.login(userName, password);
        if (user != null) {
            session.setAttribute("user",user.getuId());
            return "before/home";
        }
        return "redirect:before/login";
    }

    */


    //王志文
    @RequestMapping(value = "/login" ,method= RequestMethod.POST)
    public String UserLogin(String email,String password,HttpSession session) {
        Merchants merchants = merchantsService.login(email, password);
        if (merchants != null) {
            session.setAttribute("merchants",merchants.getEmail());
            return "back/adminmanager";
        }

        return "redirect:/page/adminlogin";
    }



    @RequestMapping(value = "zhuce",method = RequestMethod.POST)
    public String zhuce(Merchants merchants, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String email=request.getParameter("email");
        String code=request.getParameter("code");
        session=request.getSession(false);
        if(session!=null&&session.getAttribute("email")!=null&session.getAttribute("code")!=null)
        {
            String sessionEmail=(String)session.getAttribute("email");
            String sessionCode=(String)session.getAttribute("code");
            if (sessionEmail.equals(email)&&sessionCode.equals(code))
            {
                merchantsService.add(merchants);
                return "back/adminlogin";
            }
        }
        return "back/adminregister";
    }
    @RequestMapping(value="registerAuthCode",method = RequestMethod.POST)
    public void registerAuthCode(Merchants merchants, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String email=request.getParameter("email");
        Random rm=new Random();
        String code="";
        for(int i=0;i<6;i++)
        {
            int num=rm.nextInt(26)+65;
            code +=(char)num;
        }
        int res= JavaMailUtils.sendEmail(new String[]{
                email //这里就是一系列的收件人的邮箱了
        }, "商城邮箱验证", "欢迎注册商城后台，您的验证码是 <br />"+code);
        session=request.getSession(true);
        session.setAttribute("code",code);
        session.setAttribute("email",email);
        session.setMaxInactiveInterval(60);

        //merchantsService.add(merchants);
    }



    @RequestMapping(value = "/searchmerchants", method = RequestMethod.GET)
    public String merchantsPage(Model model , Integer pageSize, Integer pageNum) {
        //TODO: Add user search result
        //普通显示
        // List<user> Users = userService.listUser( pageSize,pageNum);
        //model.addAttribute("users", users);
        //return "user-list-bak";//普通页面
        //return "user-list";//adminLTE页面
        return  "/back/merchants-list";
    }
    //分页显示
    @GetMapping("/api/searchmerchants")
    @ResponseBody//需要返回json数据
    public SearchMerchantsResponse searchMerchants(Integer pageNum, Integer pageSize , String keyword){
        return  merchantsService.listMerchants(pageSize,pageNum,keyword);
    }

    @RequestMapping(value = "/addmerchants", method = RequestMethod.GET)
    public String addMerchantsPage(Model model) {
        model.addAttribute("merchants", new Merchants());
        return "/back/merchants-add";
    }

    @RequestMapping(value = "/addmerchantsb", method = RequestMethod.GET)
    public String addMerchantsPagebb(Model model) {
        model.addAttribute("merchants", new Merchants());
        return "/back/merchants-addb";
    }


    @GetMapping("/editmerchants")
    public String editMerchantsPage(@RequestParam("id") String id, Model model) {
        Merchants merchants = merchantsService.findMerchantsByPrimaryKey(id);
        model.addAttribute("merchants", merchants);
        return "/back/merchants-add";
    }

    @RequestMapping(value = "/addmerchants", method = RequestMethod.POST)
    public String saveMerchants(Merchants merchants) {
        int updated = merchantsService.saveMerchants(merchants);
        return "redirect:/merchants/searchmerchants";
    }
    @RequestMapping (value = "/deletemerchants",method=RequestMethod.GET)
    //@ResponseBody//需要返回json数据 @RequestParam("id") @PathVariable("id")
    public String deleteMerchants(@RequestParam("id") String id) {
        //var mymessage = confirm("确认删除嘛？");
        //if (mymessage == true) {
        int deleted = merchantsService.deleteMerchantsByPrimaryKey(id);

        // return deleted > 0;
        //return "redirect:/backadminuser/searchuser";//get请求不能用重定向
        return "/back/merchants-list";
        //return "redirect:/backadminuser/deleteuser";
    }
}
  /*  @DeleteMapping("/deleteuser")
    @ResponseBody
    public Boolean deleteUser(@RequestParam("id") String id) {
        int deleted = userService.deleteUserByPrimaryKey(id);

        return deleted > 0;
    }*/







